package com.jinyuanchun.learnjava.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.jinyuanchun.learnjava.service.TokenService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenServiceImpl implements TokenService {

    private static final String CLAIM_KEY_CREATED = "created";
    // 设置密钥
    @Value("${TOKEN_SECRET}")
    private  String TOKEN_SECRET;
    // 设置token有效时间为15分钟
    @Value("${EXPIRE_TIME}")
    private long EXPIRE_TIME;
    @Override
    public String getToken(String usr) {
        Date date = new Date(System.currentTimeMillis()+EXPIRE_TIME);
        String token="";
        token= JWT.create()
                .withAudience(usr)
                .withExpiresAt(date)
                .sign(Algorithm.HMAC256(TOKEN_SECRET));
        return token;
    }
}
